/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details. 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. 
*/

//compile with gcc ./NEHE-01.c -o ./nehe1 -lglut 
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <unistd.h>

#define kWindowWidth 640
#define kWindowHeight 480
#define ESCAPE 27

GLfloat rtri;
GLfloat rquad;

int window;
//Function Prototypes//

GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int Width, int Height);
void keyPressed(unsigned char key, int x, int y);

//MAIN//
int main(int argc, char** argv)
{
glutInit(&argc, argv); //inits glut with argc and argv as params
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(kWindowWidth, kWindowHeight);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("NEHE");//creates window

InitGL();//inits GL

glutKeyboardFunc(keyPressed);
glutIdleFunc(DrawGLScene);
glutDisplayFunc(DrawGLScene);//called on draw
glutReshapeFunc(ReSizeGLScene);//called on resize window

glutMainLoop();

return 0;
}
/////////


GLvoid DrawGLScene(GLvoid)
{
usleep(100000);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f);
glBegin(GL_TRIANGLES);//creates  a rgb tri
	//Front of pyramid
	glColor3f(1.0f,0.0f,0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);

	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(-1.0f,-1.0f,1.0f);

	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(1.0f,-1.0f,1.0f);

	//Right of pyramid
	glColor3f(1.0f,0.0f,0.0f);
	glVertex3f(0.0f,1.0f,0.0f);

	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(1.0f,-1.0f,1.0f);

	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(1.0f,-1.0f, -1.0f);

	//Back of pyramid
	glColor3f(1.0f,0.0f,0.0f);
	glVertex3f(0.0f,1.0f,0.0f);

	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(1.0f,-1.0f,-1.0f);

	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(-1.0f,-1.0f,-1.0f);

	//Left of pyramid
	glColor3f(1.0f,0.0f,0.0f);
	glVertex3f(0.0f, 1.0f,0.0f);

	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(-1.0f,-1.0f,-1.0f);

	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(-1.0f,-1.0f,1.0f);
	
glEnd();

glTranslatef(-2.0f,0.0f,0.0f);

glLoadIdentity();

rtri += 15.0f;

glutSwapBuffers();//must use for double buffering

}

void keyPressed(unsigned char key, int x, int y)
{
	usleep(100);

	if(key == ESCAPE)
	{
		glutDestroyWindow(window);

		exit(0);
	}
}


GLvoid ReSizeGLScene(int Width, int Height)
{
if(Height == 0)
	Height = 1;

glViewport(0, 0, Width, Height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f, (GLfloat)Width/(GLfloat)Height,0.1f, 100.0f); //sets up perspective view

glMatrixMode(GL_MODELVIEW); //returns GL to model rendering state
glLoadIdentity(); //applys perspective
}


GLvoid InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);//smooth model rendering

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//sets BG color to black

glClearDepth(1.0f);//clears depth buffer
glEnable(GL_DEPTH_TEST);//enables depth buffering
glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);//try to use nicest perspective calc
}

